package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Gsjl;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class GsjlForm extends BasePage implements Serializable {
    private GenericManager<Gsjl, Long> gsjlManager;
    private Gsjl gsjl = new Gsjl();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }


    public void setGsjlManager(GenericManager<Gsjl, Long> gsjlManager) {
        this.gsjlManager = gsjlManager;
    }

    public Gsjl getGsjl() {
        return gsjl;
    }

    public void setGsjl(Gsjl gsjl) {
        this.gsjl = gsjl;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        gsjlManager.remove(gsjl.getId());
        addMessage("gsjl.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            gsjl = gsjlManager.get(id);
        } else {
            gsjl = new Gsjl();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (gsjl.getId() == null || gsjl.getId() == 0);
        gsjl.setJbqk( jbqk );
        gsjl.setUsername(getRequest().getRemoteUser());
        gsjl.setOptdate(new Date());
        gsjl.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        gsjlManager.save(gsjl);

        String key = (isNew) ? "gsjl.added" : "gsjl.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = gsjl.getId();
            return "edit";
        }
    }
} 